package com.hall.utils;

import com.alibaba.fastjson.JSONException;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.io.IOException;

public class Sendsms {
    // 短信应用SDK AppID
    String appid = TencentMessageUtils.APP_ID;

    // 短信应用SDK AppKey
    String appkey = TencentMessageUtils.KEY_ID;

    // 短信模板ID
    String templateId = TencentMessageUtils.TEMPLATE_ID;

    // 签名内容
    String smsSign = TencentMessageUtils.SMS_SIGN;

    //腾讯云账户secretId
    String secretId= TencentMessageUtils.SECRET_ID;

    //腾讯云账户secretId
    String secretKey= TencentMessageUtils.SECRET_KEY;

    // 验证码
    String str = "";

    public void test() {
        System.out.println("短信测试+++++++++++++++++");
    }

    public String sendMessage(String phoneNum) {
        // 随即6位数赋值验证码
        String strTemp = (int) ((Math.random() * 9 + 1) * 100000) + "";
        str=strTemp;
        //模板元素
        String[] params = {strTemp, "1"};
        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            Credential cred = new Credential(secretId, secretKey);
            // 实例化一个http选项，可选的，没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");
            // 实例化一个client选项，可选的，没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            SendSmsRequest req = new SendSmsRequest();
            String[] phoneNumberSet1 = {"86" + phoneNum};
            req.setPhoneNumberSet(phoneNumberSet1);

            req.setSmsSdkAppid(appid);
            req.setSign(smsSign);
            req.setTemplateID(templateId);

            String[] templateParamSet1 = {strTemp, "1"};
            req.setTemplateParamSet(templateParamSet1);

            // 返回的resp是一个SendSmsResponse的实例，与请求对象对应
            SendSmsResponse resp = client.SendSms(req);
            // 输出json格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(resp));
        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
        return str;
    }
    //测试使用
    /*public static void main(String[] args) {
      Sendsms sms=new Sendsms();
       sms.sendMessage("13060458709");
    }*/
}

